/**
 * 
 */
package com.jushu.storbox.database;

import java.util.ArrayList;
import java.util.Calendar;
import java.util.HashMap;
import java.util.List;

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;

import com.jushu.storbox.ShareDriveApplication;
import com.jushu.storbox.constant.ClientConfig;
import com.jushu.storbox.constant.Constant;
import com.jushu.storbox.entities.FileFolderInfo;
import com.jushu.storbox.entities.FolderInfo;
import com.jushu.storbox.util.LogUtil;
import com.huawei.sharedrive.sdk.android.modelV2.response.FolderResponse;
import com.huawei.sharedrive.sdk.android.util.StringUtil;

/**
 * Folder local data access
 */
public class FolderDao extends BaseDao implements IFolderDao 
{
    public final static String LOG_TAG = "FolderDao";
    
    public FolderDao() {
    }
    
    @Override
	public FolderInfo insertFolder(FolderResponse folderResponse, int syncStu) {
    	FolderInfo folder = null;
    	try {
    		folder = new FolderInfo();
    		folder.valueOf(folderResponse);
    		folder.setSync((syncStu == Constant.DOWNLOAD_STATUS_CACHE_DB) ? true:false);
            this.getDb().insert(TABLE_NAME, null, folder.toContentValues());
        } catch (Exception e) {
            LogUtil.e(LOG_TAG, "insertFolder is fail:" + e.getLocalizedMessage());
        }
        return folder;
    }
    
    @Override
    public void insertFolder(FileFolderInfo folder) {
        this.getDb().insert(TABLE_NAME, null, folder.toContentValues());
    }
    
    @Override
	public void updateFolder(FolderResponse folderInfoResponse) {
        try {
            FolderInfo folder = new FolderInfo();
            folder.valueOf(folderInfoResponse);
            String[] whereValue = getQueryValue(folder.getId(), folder.getId());
            this.getDb().update(TABLE_NAME, folder.toContentValues(), getQueryKey(), whereValue);
        } catch (Exception e) {
            LogUtil.e(LOG_TAG, "updateFolder is fail:" + e.getLocalizedMessage());
        }
    }

    @Override
	public int updateFolder(FolderInfo folderInfo) {
        try {
            String[] whereValue = getQueryValue(folderInfo.getId(), folderInfo.getId());
            return this.getDb().update(TABLE_NAME, folderInfo.toContentValues(), getQueryKey(), whereValue);
        } catch (Exception e) {
            LogUtil.e(LOG_TAG, "updateFolder is fail:" + e.getLocalizedMessage());
        }
		return -1;
    }
    
    @Override
	public long insertOrUpdateFolder(FolderInfo folderInfo, Context context) {
        try {
            FolderInfo info = getFolderById(folderInfo.getOwnerId(), folderInfo.getId());
            if (null != info) {
                 String[] whereValue = getQueryValue(folderInfo.getId(), folderInfo.getId());
                  return this.getDb().update(TABLE_NAME, folderInfo.toContentValues(), getQueryKey(), whereValue);
            } else {
            	return this.getDb().insert(TABLE_NAME, null, folderInfo.toContentValues());
            }
        } catch (Exception e) {
            LogUtil.e(LOG_TAG, "insertFolder is fail:" + e.getLocalizedMessage());
        }
		return -1;
    }
    
    @Override
	public void insertOrUpdateFolder(FolderResponse folderInfo, Context context) {
        try {
        	FolderInfo folder = new FolderInfo();
            folder.valueOf(folderInfo);
            
            FolderInfo info = getFolderById(folderInfo.getOwnerBy(), folderInfo.getId());
            if (null != info) {
                String[] whereValue = getQueryValue(folderInfo.getId(),folderInfo.getId());
                this.getDb().update(TABLE_NAME, folder.toContentValues(), getQueryKey(), whereValue);
            } else {
                folder.setSync(false);
                this.getDb().insert(TABLE_NAME, null, folder.toContentValues());
            }
        } catch (Exception e) {
            LogUtil.e(LOG_TAG, "insertFolder is fail:" + e.getLocalizedMessage());
        }
    }

    @Override
	public List<FolderInfo> getFolderList(String ownerId, String folderId) {
        List<FolderInfo> fileInfoList = new ArrayList<FolderInfo>();
        try {
            if (null != folderId) {
                Cursor cursor = this.getDb().rawQuery("select * from " + TABLE_NAME
                    + " where "+getSubCollectQueryKey(), getQueryValue(folderId, ownerId));
                if (false == cursor.moveToFirst()) {
                    return fileInfoList;
                }
                do {
                    FolderInfo folderInfo = new FolderInfo();
                    folderInfo.fromCursor(cursor);
                    fileInfoList.add(folderInfo);
                } while (cursor.moveToNext());
            }
        } catch (Exception e) {
            LogUtil.e(LOG_TAG, "getFolderList is fail:" + e.getLocalizedMessage());
        }
        return fileInfoList;
    }
    
    @Override
	public HashMap<String, FolderInfo> getMappedFolderDatas(String ownerId, String folderId) {
    	HashMap<String, FolderInfo> fileInfoList = new HashMap<String, FolderInfo>();
    	if (StringUtil.isBlank(folderId)) {
    		return fileInfoList;
    	}
    	Cursor cursor = null;
    	try {
            cursor = this.getDb().rawQuery("select * from " + TABLE_NAME
                + " where " + getSubCollectQueryKey(), getQueryValue(folderId, ownerId));
            if(cursor == null) {
            	return fileInfoList;
            }
            
            while(cursor.moveToNext()) {
                FolderInfo folderInfo = new FolderInfo();
                folderInfo.fromCursor(cursor);
                fileInfoList.put(folderInfo.getId(), folderInfo);
            }
        } catch (Exception e) {
            LogUtil.e(LOG_TAG, "getFolderList is fail:" + e.getLocalizedMessage());
        } finally {
        	if(cursor != null) {
        		cursor.close();
        	}
        }
        return fileInfoList;
    }
    
    // flag 0:my cloud page, 1: shared page
    @Override
	public List<FolderInfo> getFolderListPage(String ownerId, String folderId, int flag) {
        if (null != folderId) {
            int number = ClientConfig.CLOUD_COUNT_PER_PAGE;
            int count = ShareDriveApplication.getInstance().getMyCloudDrivePage() * number;
            if (Constant.SHARED_GET_FILE_LIST_STATE == flag) {
                number = ClientConfig.SHARED_COUNT_PER_PAGE;//
                count = ShareDriveApplication.getInstance().getMySharePage() * number;
            }
            
            List<FolderInfo> fileInfoList = new ArrayList<FolderInfo>();
            try {
                if (null != folderId) {
                    Cursor cursor = this.getDb()
                        .rawQuery("select * from " + TABLE_NAME
                            + " where "+getSubCollectQueryKey()+" Order by server_mtime desc" + " limit ?"
                            + " offset ?",
                            new String[]{folderId, ownerId, getCurrentUserId(),
                        		String.valueOf(number), String.valueOf(count)});
                    if (false == cursor.moveToFirst()) {
                        return fileInfoList;
                    }
                    do {
                        FolderInfo folderInfo = new FolderInfo();
                        folderInfo.fromCursor(cursor);
                        fileInfoList.add(folderInfo);
                    } while (cursor.moveToNext());
                }
            } catch (Exception e) {
                LogUtil.e(LOG_TAG, "getFolderListPage is fail:" + e.getLocalizedMessage());
            }
            return fileInfoList;
        } else {
            LogUtil.e(LOG_TAG, "folderId is null");
        }
        return null;
    }
    
    @Override
	public List<FolderInfo> getFolderListByPage(int pageNo, int number, String ownerId, String folderId, String des, String orderBy) {
        String dbDes = "desc";
        String dbOrderBy = "server_mtime";
        if ("DESC".equals(des)) {
            dbDes = "desc";
        } else {
            dbDes = "asc";
        }
        if ("size".equals(orderBy)) {
            dbOrderBy = file_size;
        } else if ("name".equals(orderBy)) {
            dbOrderBy = file_name;
        } else {
            dbOrderBy = server_mtime;
        }
        if (null != folderId) {
            int offset = (pageNo -1) * ClientConfig.CLOUD_COUNT_PER_PAGE;
            List<FolderInfo> fileInfoList = new ArrayList<FolderInfo>();
            try {
                if (null != folderId) {
                	String strSQL = "select * from " + TABLE_NAME;
                	if ("name".equals(orderBy)){
                		strSQL = "select * from " + TABLE_NAME
                                + " where "+getSubCollectQueryKey()
                				+ " Order by " 
                				+ dbOrderBy + " collate nocase " + dbDes 
                				+ " limit ?"
                                + " offset ?";
                	} else {
                		strSQL = "select * from " + TABLE_NAME
                        + " where "+getSubCollectQueryKey()
                		+ " Order by " + dbOrderBy + " " + dbDes + " limit ?"
                        + " offset ?";
                	}
                    Cursor cursor = this.getDb().rawQuery(strSQL,
                    							new String[]{
                    									folderId, ownerId, getCurrentUserId(),
                    									String.valueOf(number), 
                    									String.valueOf(offset)});
                    while (cursor.moveToNext()) {
                        FolderInfo folderInfo = new FolderInfo();
                        folderInfo.fromCursor(cursor);
                        fileInfoList.add(folderInfo);
                    }
                }
            } catch (Exception e) {
                LogUtil.e(LOG_TAG, "getFolderListPage is fail:" + e.getLocalizedMessage());
            }
            return fileInfoList;
        } else {
            LogUtil.e(LOG_TAG, "folderId is null");
        }
        return null;
    }
    
    
    @Override
	@Deprecated
    public List<FolderInfo> getFolderListPage(int flag, String ownerId, String folderId, String des, String orderBy)
    {
        String dbDes = "desc";
        String dbOrderBy = "server_mtime";
        if ("DESC".equals(des)) {
            dbDes = "desc";
        } else {
            dbDes = "asc";
        }
        if ("size".equals(orderBy)) {
            dbOrderBy = file_size;
        } else if ("name".equals(orderBy)) {
            dbOrderBy = file_name;
        } else {
            dbOrderBy = server_mtime;
        }
        if (null != folderId) {
            int number = ClientConfig.CLOUD_COUNT_PER_PAGE;
            int count = ShareDriveApplication.getInstance().getMyCloudDrivePage() * number;
            if (Constant.SHARED_GET_FILE_LIST_STATE == flag) {
                number = ClientConfig.SHARED_COUNT_PER_PAGE;//
                count = ShareDriveApplication.getInstance().getMySharePage() * number;
            }
            
            List<FolderInfo> fileInfoList = new ArrayList<FolderInfo>();
            try {
                if (null != folderId) {
                	String strSQL = "select * from " + TABLE_NAME;
                	if ("name".equals(orderBy)){
                		strSQL = "select * from " + TABLE_NAME
                                + " where "+ getSubCollectQueryKey()
                				+ " Order by " 
                				+ dbOrderBy + " collate nocase " + dbDes 
                				+ " limit ?"
                                + " offset ?";
                	} else {
                		strSQL = "select * from " + TABLE_NAME
                        + " where "+getSubCollectQueryKey()
                		+ " Order by " + dbOrderBy + " " + dbDes + " limit ?"
                        + " offset ?";
                	}
                    Cursor cursor = this.getDb().rawQuery(strSQL,
                    							new String[]{
                    									folderId, ownerId, getCurrentUserId(),
                    									String.valueOf(number), 
                    									String.valueOf(count)});
                    while (cursor.moveToNext()) {
                        FolderInfo folderInfo = new FolderInfo();
                        folderInfo.fromCursor(cursor);
                        fileInfoList.add(folderInfo);
                    } 
                }
            } catch (Exception e) {
                LogUtil.e(LOG_TAG, "getFolderListPage is fail:" + e.getLocalizedMessage());
            }
            return fileInfoList;
        } else {
            LogUtil.e(LOG_TAG, "folderId is null");
        }
        return null;
    }
    
    @Override
	public void deleteFolder(String ownerId, String folderId) {
        try {
            if (null != folderId) {
                this.getDb().execSQL("delete from " + TABLE_NAME + " where " + getQueryKey(), 
                		getQueryValue(folderId, ownerId));
            }
        } catch (Exception e) {
            LogUtil.e(LOG_TAG, "deleteFolder is fail:" + e.getLocalizedMessage());
        }
    }
    
    @Override
	public FolderInfo getFolderById(String ownerId, String folderId) {
        FolderInfo folderInfo = null;
        try {
            if (null != folderId) {
            	String sql = "select * from " + TABLE_NAME + " where "+ getQueryKey() + " order by "+IBaseDao.id+" desc";
            	Cursor cursor = this.getDb().rawQuery(sql, getQueryValue(folderId, ownerId));
                if (cursor.moveToFirst()) {
                    folderInfo = new FolderInfo();
                    folderInfo.fromCursor(cursor);
                }
            }
        } catch (Exception e) {
            LogUtil.e(LOG_TAG, "getFolderById is fail:" + e.getLocalizedMessage());
        }
        return folderInfo;
    }
    
    @Override
	public void updateLocalPath(String ownerId, String folderId, String localPath) {
        try {
        	ContentValues cv = new ContentValues();
            cv.put(IBaseDao.local_path, localPath);
            this.getDb().update(TABLE_NAME, cv, getQueryKey(), getQueryValue(folderId, ownerId));
        } catch (Exception e) {
            LogUtil.e(LOG_TAG, "updateLocalPath is fail:" + e.getLocalizedMessage());
        }
    }
    
    @Override
	public void updateTransStatus(String ownerId, String folderId, int status) {
        try {
            ContentValues cv = new ContentValues();
            cv.put(IBaseDao.trans_status, status);
            this.getDb().update(TABLE_NAME, cv, getQueryKey(), getQueryValue(folderId, ownerId));
        } catch (Exception e) {
            LogUtil.e(LOG_TAG, "updateTransStatus is fail:" + e.getLocalizedMessage());
        }
    }
    
    @Override
	public void updateParent(String ownerId, String folderId,String parentId) {
    	try {
            ContentValues cv = new ContentValues();
            cv.put(IBaseDao.parent_folder_id, parentId);
            this.getDb().update(TABLE_NAME, cv, getQueryKey(), getQueryValue(folderId, ownerId));
        } catch (Exception e) {
            LogUtil.e(LOG_TAG, "updateParent is fail:" + e.getLocalizedMessage());
        }
    }
    
    @Override
	public int updateSyncStatuc(String ownerId, String folderId, int syncStatus) {
        try {
            String[] whereValue = getQueryValue(folderId, ownerId);
            ContentValues cv = new ContentValues();
            cv.put(isSync, syncStatus);
            return this.getDb().update(TABLE_NAME, cv, getQueryKey(), whereValue);
        } catch (Exception e) {
            LogUtil.e(LOG_TAG, "updateSyncStatuc is fail:" + e.getLocalizedMessage());
        }
        return -1;
    }
    
    @Override
	public void updateLastRefreshTime(String ownerId, String folderId, long lastRefreshTime) {
        try {
            if (null != folderId) {
            	ContentValues cv = new ContentValues();
            	cv.put(IBaseDao.last_refresh_time, lastRefreshTime);
            	this.getDb().update(TABLE_NAME, cv, getQueryKey(), getQueryValue(folderId, ownerId));
            }
        } catch (Exception e) {
            LogUtil.e(LOG_TAG, "updateLastRefreshTime is fail:" + e.getLocalizedMessage());
        }
    }
    
    @Override
	public long getLastRefreshTime(String ownerId, String folderId) {
        long lastRefreshTime = 0;
        try {
            if (null != folderId) {
                Cursor cursor = this.getDb().rawQuery("select * from  " + TABLE_NAME 
                		+ " where "+getQueryKey(), getQueryValue(folderId, ownerId));
                if (cursor.moveToFirst()) {
                    lastRefreshTime = cursor.getLong(cursor.getColumnIndex(IBaseDao.last_refresh_time));
                }
            }
        } catch (Exception e) {
            LogUtil.e(LOG_TAG, "getLastRefreshTime is fail:" + e.getLocalizedMessage());
        }
        return lastRefreshTime;
    }
    
    @Override
	public long getLastUpdateTime(String ownerId, String folderId) {
        long lastUpdateTime = 0;
        try {
            if (null != folderId) {
                Cursor cursor = this.getDb().rawQuery("select * from " + TABLE_NAME 
                		+ " where "+getQueryKey(), getQueryValue(folderId, ownerId));
                if (cursor.moveToFirst()) {
                    lastUpdateTime = cursor.getLong(cursor.getColumnIndex(IBaseDao.local_last_modify_time));
                }
            }
        } catch (Exception e) {
            LogUtil.e(LOG_TAG, "getLastUpdateTime is fail:" + e.getLocalizedMessage());
        }
        return lastUpdateTime;
    }
    
    @Override
	public void updateLastUpdateTime(String ownerId, String folderId) {
        try {
            if (null != folderId) {
            	ContentValues cv = new ContentValues();
            	cv.put(IBaseDao.local_last_modify_time, Calendar.getInstance().getTimeInMillis());
				this.getDb().update(TABLE_NAME, cv , getQueryKey(), getQueryValue(folderId, ownerId));
            }
        } catch (Exception e) {
            LogUtil.e(LOG_TAG, "updateLastUpdateTime is fail:" + e.getLocalizedMessage());
        }
    }
    
    @Override
    public void updateLocalSaveState(FileFolderInfo folderInfo) {
    	try {
            if (null != folderInfo.getId()) {
				this.getDb().update(TABLE_NAME, folderInfo.getSyncStateCV(),
						getQueryKey(), getQueryValue(folderInfo.getId(), folderInfo.getOwnerBy()));
            }
        } catch (Exception e) {
            LogUtil.e(LOG_TAG, "updateLocalSaveState is fail:" + e.getLocalizedMessage());
        }
    }
    
    @Override
	public void rename(String name, FolderResponse folderResponse, FileFolderInfo folder)  {
    	rename(name, folderResponse, folder.getOwnerId(), folder.getId());
    }
    @Override
	public void rename(String name, FolderResponse folderResponse, String ownerId, String folderID) {
        try  {
            if (null != folderID) {
            	ContentValues cv = new ContentValues();
            	cv.put(IBaseDao.file_name, name);
            	cv.put(IBaseDao.server_mtime, folderResponse.getModifiedAt());
				this.getDb().update(TABLE_NAME, cv , getQueryKey(), getQueryValue(folderID, ownerId));
            }
        } catch (Exception e) {
            LogUtil.e(LOG_TAG, "renameFolder is fail:" + e.getLocalizedMessage());
        }
    }
    
    @Override
	public void move(FileFolderInfo folder, FileFolderInfo targetFolder) {
    	move(folder.getOwnerId(), targetFolder.getId(), folder.getId());
    }
    @Override
	public void move(String ownerId, String parentID, String folderID) {
        try {
            if (null != folderID) {
                ContentValues cv = new ContentValues();
            	cv.put(IBaseDao.parent_folder_id, parentID);
				this.getDb().update(TABLE_NAME, cv, getQueryKey(), getQueryValue(folderID, ownerId));
            }
        } catch (Exception e) {
            LogUtil.e(LOG_TAG, "moveFolder is fail:" + e.getLocalizedMessage());
        }
    }
    
    @Override
	public void deleteAll() {
        try {
			String where = IFolderDao.CURRENT_USER_ID +"=?";
            String[] whereArgs = new String[]{ getCurrentUserId() };
			this.getDb().delete(TABLE_NAME, where, whereArgs);
        } catch (Exception e) {
            LogUtil.e(LOG_TAG, "deleteAll is fail:" + e.getLocalizedMessage());
        }
    }

    @Override
	public FolderInfo getFolderByName(FileFolderInfo folder, String folderName) {
    	return getFolderByName(folder.getOwnerId(), folderName);
    }
    @Override
	public FolderInfo getFolderByName(String ownerId, String folderName) {
        FolderInfo folderInfo = null;
        try {
            if (null != folderName) {
            	Cursor cursor = this.getDb().rawQuery("select * from " + TABLE_NAME + " where "
                        + file_name + "=? and "+IBaseDao.owner_by +"=? and "
                    	+ IFolderDao.CURRENT_USER_ID+"=? order by "+IBaseDao.id+" desc",
                        new String[]{folderName, ownerId, getCurrentUserId()});
                if (cursor.moveToFirst()) {
                    folderInfo = new FolderInfo();
                    folderInfo.fromCursor(cursor);
                    boolean isSynca = false;
                    int sync = cursor.getInt(cursor.getColumnIndex(isSync));
                    if (sync == 1) {
                        isSynca = true;
                    }
                    folderInfo.setSync(isSynca);
                }
            }
        } catch (Exception e) {
            LogUtil.e(LOG_TAG, "getFolderByName is fail:" + e.getLocalizedMessage());
        }
        return folderInfo;
    }

    @Override
	public List<FolderInfo> getSearchFolderList(FileFolderInfo folder, String fileName) {
    	return getSearchFolderList(folder.getId(), fileName);
    }
    @Override
	public List<FolderInfo> getSearchFolderList(String ownerId, String fileName) {
        List<FolderInfo> fileInfoList = new ArrayList<FolderInfo>();
        try {
            Cursor cursor = this.getDb().rawQuery("select * from " + TABLE_NAME + " where "
                + IBaseDao.owner_by +"=? and "
            	+ IFolderDao.CURRENT_USER_ID+" = ? and "
            	+ IBaseDao.file_name + " like ?",
                new String[]{ownerId, getCurrentUserId(), "%" + fileName + "%"});
            while (cursor.moveToNext()) {
                FolderInfo folderInfo = new FolderInfo();
                folderInfo.fromCursor(cursor);
                fileInfoList.add(folderInfo);
            }
        } catch (Exception e) {
            LogUtil.e(LOG_TAG, "getFolderList is fail:" + e.getLocalizedMessage());
        }
        return fileInfoList;
    }

    @Override
	public void setChangedFolderId(FileFolderInfo folder, String changeFolderId) {
    	setChangedFolderId(folder.getOwnerId(), folder.getId(), changeFolderId);
    }
    @Override
	public void setChangedFolderId(String ownerId, String folderId, String changeFolderId) {
        try {
            if (null != folderId) {
            	ContentValues cv = new ContentValues();
            	cv.put(IBaseDao.changed_folderId, changeFolderId);
				this.getDb().update(TABLE_NAME, cv , getQueryKey(), getQueryValue(folderId, ownerId));
            }
        } catch (Exception e) {
            LogUtil.e(LOG_TAG, "updateLastRefreshTime is fail:" + e.getLocalizedMessage());
        }
    }

    @Override
	public String getChangedFolderId(FileFolderInfo folder) {
    	return getChangedFolderId(folder.getOwnerId(), folder.getId());
    }
    @Override
	public String getChangedFolderId(String ownerId, String folderId) {
        String changedFolderId = "0";
        try {
            if (null != folderId) {
                Cursor cursor = this.getDb().rawQuery("select * from  " + TABLE_NAME 
                		+ " where " + getQueryKey(), getQueryValue(folderId, ownerId));
                if (cursor.moveToFirst()) {
                    changedFolderId = cursor.getString(cursor.getColumnIndex(IBaseDao.changed_folderId));
                }
            }
        } catch (Exception e) {
            LogUtil.e(LOG_TAG, "getLastRefreshTime is fail:" + e.getLocalizedMessage());
        }
        return changedFolderId;
    }

    @Override
	public void setFolderLink(FileFolderInfo folder, boolean isShareLink) {
    	setFolderLink(folder.getOwnerId(),folder.getId(), isShareLink);
    }
    @Override
	public void setFolderLink(String ownerId, String folderId, boolean isShareLink) {
        try {
            if (null != folderId) {
                ContentValues cv = new ContentValues();
                cv.put(IBaseDao.isshare_link, isShareLink);
                this.getDb().update(TABLE_NAME, cv, getQueryKey(), getQueryValue(folderId, ownerId));
            }
        } catch (Exception e) {
            LogUtil.e(LOG_TAG, "updateShareLink is fail:" + e.getLocalizedMessage());
        }
    }

	@Override
	public void cleanCatch(){
		try {
			ContentValues values = new ContentValues();
			values.put(IBaseDao.trans_status, Constant.DOWNLOAD_NON_UPDATE_DB);
			values.put(IBaseDao.isSync, Constant.DOWNLOAD_STATUS_NOCACHE_DB);
			String whereClause = IFolderDao.CURRENT_USER_ID + "=?";
			String[] whereArgs = {getCurrentUserId()};
			this.getDb().update(TABLE_NAME, values, whereClause, whereArgs);
		} catch (Exception e) {
			LogUtil.e(LOG_TAG, "deleteAll is fail:" + e.getLocalizedMessage());
		}
	}

	
	@Override
	public long getLastUpdateTime(String objectId) {
		return 0;
	}

	@Override
	public void updateLastUpdateTime(String objectId) {
	}

	@Override
	public FolderInfo getFolderById(FileFolderInfo folder) {
		return getFolderById(folder.getOwnerId(), folder.getId());
	}

	@Override
	public FolderInfo getFolderById(FolderResponse frServer) {
		return getFolderById(frServer.getOwnerBy(), frServer.getId());
	}

	@Override
	public void updateSyncStatuc(FolderResponse frServer,
			int downloadStatusCacheDb) {
		updateSyncStatuc(frServer.getOwnerBy(), frServer.getId(), downloadStatusCacheDb);
	}

	@Override
	public void rename(FolderResponse folderResponse,
			FileFolderInfo fileInformation) {
		rename(name, folderResponse, folderResponse.getOwnerBy(), folderResponse.getId());
	}
	
	/**id=? and owner_by=? and CURRENT_USER_ID=?*/
	private String getQueryKey() {
		return IBaseDao.id + "=? and "+ IBaseDao.owner_by +"=? and " +IFileDao.CURRENT_USER_ID+"=?";
	}

	/**parent_folder_id=? and owner_by=? and CURRENT_USER_ID=?*/
	private String getSubCollectQueryKey() {
		return IBaseDao.parent_folder_id + "=? and "+ IBaseDao.owner_by +"=? and " +IFileDao.CURRENT_USER_ID+"=?";
	}
	
	private String[] getQueryValue(String folderId, String ownerId) {
		return new String[] { folderId , ownerId, getCurrentUserId()};
	}

	private String getCurrentUserId() {
		return ShareDriveApplication.getInstance().getWnerID();
	}
}
